package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.GeoRegion;
import com.tbit.uqbike.object.pojo.GeoRegionBound;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.business.GeoRegionService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.GeoRegionBoundDao;
import com.tbit.uqbike.webmanager.dao.core.GeoRegionDao;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 围栏分区(GeoRegion)表控制层
 *
 * @author jing
 * @since 2020-11-23 16:36:01
 */
@RestController
@RequestMapping("/georegion")
@Slf4j
public class GeoRegionController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private GeoRegionService geoRegionService;
    @Resource
    private CalibrateService calibrateService;
    @Resource
    private AccountDao accountDao;
    @Resource
    private GeoRegionDao geoRegionDao;
    @Resource
    private GeoRegionBoundDao geoRegionBoundDao;

    /**
     * 根据分区id查询分区信息
     *
     * @param regionId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer regionId) {
        Assert.notNull(regionId, "ver.regionId.notNull");
        return JsonResult.ok(this.geoRegionService.queryById(regionId));
    }

    /**
     * @param geoRegion
     * @param mapType
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 添加分区，存在分区数据即修改(此方法存在主键，所以一个方法可以进行新增和删除操作)
     * @createDate 2020/11/23 18:08
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.REGION, methods = MethodEnum.GEOREGION_ADD)
    public JsonResult add(GeoRegion geoRegion, @RequestParam(defaultValue = "2") Integer mapType) {
        Assert.notNull(geoRegion.getPoints(), "ver.points.notNull");
        Assert.notNull(geoRegion.getRegionName(), "ver.name.notNull");
        if (geoRegion.getRegionName().length() > 50){
            throw new BaseException("分区名称过长");
        }
        Account account = accountDao.getByAccountId(geoRegion.getAccountId());
        if (null == account) {
            throw new BaseException(SystemCodeEnum.ACCOUNT_ID_NOT_VALID);
        }
        //限制经纬度集合不能超过数据库设定的字段长度
        //if (geoRegion.getPoints().length() > 500) {
        //    throw new BaseException("绘制的点数过多，请重新绘制");
        //}
        geoRegion.setPointsC(geoRegion.getPoints());
        /**经纬度校准*/
        calibrateService.calVert(geoRegion, mapType, MapConstant.MAP_ORI);
        geoRegion = geoRegionService.insert(geoRegion);
        calibrateService.calVert(geoRegion, MapConstant.MAP_ORI, mapType);
        double lng = geoRegion.getCenterLon();
        double lat = geoRegion.getCenterLat();
        Point p = calibrateService.calibrateOffSet(lng, lat, mapType);
        geoRegion.setCenterLon(Double.parseDouble(new DecimalFormat("#.######").format(p.getX())));
        geoRegion.setCenterLat(Double.parseDouble(new DecimalFormat("#.######").format(p.getY())));
        return JsonResult.ok(geoRegion);


    }


    /**
     * 删除分区
     */
    @PostMapping("/delete")
    @OperateLog(module = ModuleEnum.REGION, methods = MethodEnum.GEOREGION_DEL)
    public JsonResult delete(GeoRegion geoRegion) {
        Assert.notNull(geoRegion.getRegionId(), "ver.regionId.notNull");
        Account account = accountDao.getByAccountId(geoRegion.getAccountId());
        if (null == account) {
            throw new BaseException(SystemCodeEnum.ACCOUNT_ID_NOT_VALID);
        }
        geoRegionService.deleteById(geoRegion.getRegionId());
        return JsonResult.ok();
    }

    /**
     * @param geoRegion
     * @param mapType
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 分页查询
     * @createDate 2020/11/26 19:01
     */
    @PostMapping("/queryByGeoregion")
    public JsonResult queryByGeoRegion(GeoRegion geoRegion, @RequestParam(defaultValue = "2") Integer mapType, HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        Account account = accountDao.getByAccountId(geoRegion.getAccountId());
        if (null == account) {
            throw new BaseException(SystemCodeEnum.ACCOUNT_ID_NOT_VALID);
        }
        if (geoRegion.getPageNO() == null && geoRegion.getRowCount() == null) {
            geoRegion.setPageNO(1);
            geoRegion.setRowCount(10000);
        }
        geoRegion.setPage(new Page(geoRegion.getPageNO(), geoRegion.getRowCount()));
        PageHelperUtil.initPage(geoRegion.getPage());
        List<GeoRegion> list = new ArrayList<>();
        List<GeoRegionBound> geoRegionBoundList = geoRegionBoundDao.selectByUserId(info.accountUserId);
        /**登录的账户绑定了分区*/
        if (geoRegionBoundList != null && geoRegionBoundList.size() != 0) {
            for (GeoRegionBound geoRegionBound : geoRegionBoundList) {
                list.add(geoRegionDao.getGeoRegion(geoRegionBound.getRegionId()));
            }
        } else {
            list = geoRegionService.queryByGeoRegion(geoRegion);
        }
        /**经纬度校准*/
        for (GeoRegion geoRegion1 : list) {
            calibrateService.calVert(geoRegion1, MapConstant.MAP_ORI, mapType);
        }
        if (list.size() > 0) {
            calibrateService.celibrateGeoRegion(list, mapType);
        }
        PageInfo pageInfo = new PageInfo(list);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }


}